// <copyright file="UriReferenceComparer.cs" company="Alan Dean">
// The MIT License

// Copyright (c) 2008 Alan Dean

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// </copyright>
// <author>Alan Dean</author>
// <email>alan.dean@thoughtpad.net</email>

namespace Semantic.Collections
{
    using System;
    using System.Collections;
    using System.Globalization;

    using Semantic.Rdf;

    /// <summary>
    /// Provides comparison functionality for <see cref="T:Semantic.Rdf.UriReference"/> types.
    /// <para>This class cannot be inherited.</para>
    /// </summary>
    public sealed class UriReferenceComparer : IComparer
    {
        #region public UriReferenceComparer()
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Semantic.Rdf.UriReferenceComparer"/> class.
        /// </summary>
        public UriReferenceComparer()
        {
        }
        #endregion

        #region public int Compare(object, object)
        /// <summary>
        /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
        /// </summary>
        /// 
        /// <param name="x">The first object to compare.</param>
        /// <param name="y">The second object to compare.</param>
        /// 
        /// <returns>
        /// <list type="table">
        /// <listheader>
        ///   <term>Value</term>
        ///   <description>Condition</description>
        /// </listheader>
        /// <item>
        ///   <term>Less than zero</term>
        ///   <description><paramref name="x"/> is less than <paramref name="y"/>.</description>
        /// </item>
        /// <item>
        ///   <term>Zero</term>
        ///   <description><paramref name="x"/> equals <paramref name="y"/>.</description>
        /// </item>
        /// <item>
        ///   <term>Greater than zero</term>
        ///   <description><paramref name="x"/> is greater than <paramref name="y"/>.</description>
        /// </item>
        /// </list>
        /// </returns>
        /// 
        /// <exception cref="T:System.ArgumentException">
        /// Neither <paramref name="x"/> nor <paramref name="y"/>
        /// implements the <see cref="T:System.IComparable"/> interface.<br/>-or-<br/>
        /// <paramref name="x"/> and <paramref name="y"/> are of different types
        /// and neither one can handle comparisons with the other.
        /// </exception>
        public int Compare(object x, object y)
        {
            if (!(x is IComparable))
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.InvariantCulture, Properties.Resources.ObjectMustBeOfTypeException_Message_Format, "IComparable"),
                    "x");
            }

            if (!(y is IComparable))
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.InvariantCulture, Properties.Resources.ObjectMustBeOfTypeException_Message_Format, "IComparable"),
                    "y");
            }

            IUriReference comparand1 = (x as IUriReference);
            IUriReference comparand2 = (y as IUriReference);

            return object.ReferenceEquals(comparand1, comparand2)
                ? 0
                : string.Compare(
                    object.ReferenceEquals(null, comparand1) ? null as string : comparand1.LexicalForm,
                    object.ReferenceEquals(null, comparand2) ? null as string : comparand2.LexicalForm,
                    StringComparison.OrdinalIgnoreCase);
        }
        #endregion
    }
}